package com.example.litemall.admin.api.service.impl;

import com.example.litemall.admin.api.commons.MapperUtils;
import com.example.litemall.admin.api.domain.LitemallPermission;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.litemall.admin.api.mapper.LitemallPermissionMapper;
import com.example.litemall.admin.api.service.LitemallPermissionService;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class LitemallPermissionServiceImpl implements LitemallPermissionService{

    @Resource
    private LitemallPermissionMapper litemallPermissionMapper;

    @SneakyThrows
    @Override
    public Set<String> getPermissionsByIds(String ids) {
        Set<String> permissions = new HashSet<>();
        if (ids.isEmpty()) {
            return permissions;
        }
        List<Integer> integers = MapperUtils.json2list(ids, Integer.class);

        Example example = new Example(LitemallPermission.class);
        example.or().andIn("roleId",integers).andEqualTo("deleted",false);

        List<LitemallPermission> litemallPermissions = litemallPermissionMapper.selectByExample(example);
        for (LitemallPermission litemallPermission : litemallPermissions) {
            permissions.add(litemallPermission.getPermission());
        }

        return permissions;
    }
}
